﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demos.DesignPatterns._05_建造者模式.Services
{
    /// <summary>
    /// 订单构建器 
    /// </summary>
    public class StandardOrderBuilder : IOrderBuilder
    {

        private OrderInfo _orderInfo = new OrderInfo();

        /// <summary>
        /// 设置订单信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="createTime"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder SetOrderInfo(string orderId, DateTime createTime, string status)
        {
            _orderInfo.OrderId = orderId;
            _orderInfo.CreateTime = createTime;
            _orderInfo.Status = status;
            return this;
        }

        /// <summary>
        /// 设置客户信息
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="name"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder SetCustomer(string customerId, string name, string email)
        {
            _orderInfo.Customer = new Customer
            {
                CustomerId = customerId,
                Name = name,
                Email = email
            };
            return this;
        }

        /// <summary>
        /// 添加订单项
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder AddItem(string productId, string name, decimal price, int quantity)
        {
            _orderInfo.Items.Add(new OrderItem
            {
                ProductId = productId,
                ProductName = name,
                UnitPrice = price,
                Quantity = quantity
            });
            return this;
        }

        /// <summary>
        /// 设置支付信息
        /// </summary>
        /// <param name="method"></param>
        /// <param name="amount"></param>
        /// <param name="transactionId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder SetPayment(string method, decimal amount, string transactionId)
        {
            _orderInfo.Payment = new PaymentInfo
            {
                Method = method,
                Amount = amount,
                TransactionId = transactionId
            };
            return this;
        }

        /// <summary>
        /// 设置配送信息
        /// </summary>
        /// <param name="method"></param>
        /// <param name="address"></param>
        /// <param name="receiver"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder SetShipping(string method, string address, string receiver, string phone)
        {
            _orderInfo.Shipping = new ShippingInfo
            {
                Method = method,
                Address = address,
                Receiver = receiver,
                Phone = phone
            };
            return this;
        }

        /// <summary>
        /// 设置折扣信息
        /// </summary>
        /// <param name="description"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder SetDiscount(string description, decimal amount)
        {
            _orderInfo.Discount = new DiscountInfo
            {
                Description = description,
                Amount = amount
            };
            return this;
        }

        /// <summary>
        /// 设置发票信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="title"></param>
        /// <param name="taxNumber"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IOrderBuilder SetInvoice(string type, string title, string taxNumber)
        {
            _orderInfo.Invoice = new InvoiceInfo
            {
                Type = type,
                Title = title,
                TaxNumber = taxNumber
            };
            return this;
        }

        /// <summary>
        /// 构建订单
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public OrderInfo Build()
        {
            /*
             * 在这里配置订单的构建逻辑，校验信息
             * **/
            if (_orderInfo.Items.Count == 0)
                throw new Exception("订单必须包含至少一个商品");
            if (_orderInfo.Payment == null)
                throw new Exception("订单必须包含支付信息");
            if (_orderInfo.Customer == null)
                throw new Exception("订单必须包含客户信息");
            return _orderInfo;
        }

    }
}
